package com.jmb.apiplatform.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.RoleMapper;
import com.jmb.apiplatform.pojo.PermissionRoleDao;
import com.jmb.apiplatform.pojo.RoleDao;
import com.jmb.apiplatform.pojo.RoleVO;
import com.jmb.apiplatform.service.RoleService;
import com.jmb.apiplatform.util.ValidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public String findRoleName(String name) {
        return roleMapper.findRoleName(name);
    }

    @Override
    public void saveRole(RoleDao roleDao) throws ValidException {
        ValidUtil.notNUll(roleDao.getRoleName(),"角色名不能为空");
        ValidUtil.length(roleDao.getRoleName(),1,20,"角色名长度在1-20之间");
        String roleName = roleMapper.findRoleName(roleDao.getRoleName());
        if (roleName!=null){
            throw new ValidException("角色名已存在,请检查重试");
        }
        ValidUtil.notNUll(roleDao.getStatus(),"角色状态不能为空");
        roleMapper.insertRole(roleDao);

    }

    @Override
    public PageResult<RoleDao> roleList(RoleVO roleVO) {
        PageHelper.startPage(roleVO.getPageNum(), roleVO.getPageSize());
        List<RoleDao> rolelist = roleMapper.rolelist(roleVO);
        Page<RoleDao> p = (Page<RoleDao>) rolelist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public RoleDao findId(Integer id) {
        return roleMapper.findId(id);
    }

    @Override
    public void editRole(RoleDao roleDao) throws ValidException {
        String roleName = roleDao.getRoleName();
        String renameTrim = roleName.trim();
        ValidUtil.notNUll(renameTrim, "角色名不能为空");
        ValidUtil.length(renameTrim, 1, 20, "角色名长度在1-20之间");
        roleDao.setRoleName(renameTrim);
        roleMapper.editRole(roleDao);
    }

    @Override
    public void removeId(Integer id) throws ValidException {
        ValidUtil.notNUll(id,"角色id不能为空");
        roleMapper.removeId(id);
    }

    @Override
    public List<RoleDao> allStatus(Integer status) throws ValidException {
        if (status!= 1 && status!= 0){
            throw new ValidException("角色状态只能是0或1");
        }
        return roleMapper.allStatus(status);

    }

    @Override
    public String findIdName(Integer roleId) throws ValidException {
        if (roleId == null) {
            throw new ValidException("角色id不能为空");
        }
        String idName = roleMapper.findIdName(roleId);
        return idName;
    }

    @Override
    public List<Integer> checkpermissionId(Integer id) throws ValidException {
        if (id == null) {
          throw new ValidException("角色id不能为空");
        }
        List<Integer> listId = roleMapper.checkPermissionId(id);

        return listId;
    }

    @Override
    public void saveRolePermission(PermissionRoleDao permissionRoleDao) throws ValidException {
        ValidUtil.notNUll(permissionRoleDao.getRoleId(),"角色id不能为空");
        ValidUtil.notNUll(permissionRoleDao.getPermissionId(),"权限id不能为空");
        roleMapper.saveRolePermission(permissionRoleDao);
    }

    @Override
    public void removePermissionRole(PermissionRoleDao permissionRoleDao) throws ValidException {
        ValidUtil.notNUll(permissionRoleDao.getRoleId(),"角色id不能为空");
        ValidUtil.notNUll(permissionRoleDao.getPermissionId(),"权限id不能为空");
        roleMapper.removePerssionRole(permissionRoleDao);
    }

    @Override
    public List<String> readPermission(Integer roleId) throws ValidException {
        if (roleId==null){
            throw new ValidException("角色id不能为空");
        }
        List<String> permissions = roleMapper.readPermission(roleId);
        return permissions;
    }
}




